package com.mini.mysql.service;

import com.mini.mysql.model.BusinessStudentInfo;
import com.mini.mysql.repository.BusinessStudentInfoRepository;
import com.mini.util.BeanMapUtils;
import com.mini.vo.BusinessStudentInfoVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageImpl;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author anjl
 * @version 1.0
 * @date Created in 2019/12/5
 */
@Service
@Slf4j
public class BusinessStudentInfoService  {

    @Autowired
    private BusinessStudentInfoRepository businessStudentInfoRepository;

    public Page<BusinessStudentInfoVO> page(Date orderStartTime, Date orderEndTime,
                                            Pageable pageable) {
        Page<Map<String, Object>> page = businessStudentInfoRepository.page(orderStartTime, orderEndTime,
                pageable);

        List<BusinessStudentInfoVO> collect = page.getContent().stream().map(o -> {
            try {
                return BeanMapUtils.mapToBean(o, BusinessStudentInfoVO.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }).collect(Collectors.toList());
        return new PageImpl<>(collect, pageable, page.getTotalElements());
    }

    public Page<BusinessStudentInfoVO> pageAttach(Date orderStartTime, Date orderEndTime,
                                            Pageable pageable) {
        Page<Map<String, Object>> page = businessStudentInfoRepository.pageAttach(orderStartTime, orderEndTime,
                pageable);

        List<BusinessStudentInfoVO> collect = page.getContent().stream().map(o -> {
            try {
                return BeanMapUtils.mapToBean(o, BusinessStudentInfoVO.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }).collect(Collectors.toList());
        return new PageImpl<>(collect, pageable, page.getTotalElements());
    }

    public Page<BusinessStudentInfoVO> pageClass(Date orderStartTime, Date orderEndTime,
                                                  Pageable pageable) {
        Page<Map<String, Object>> page = businessStudentInfoRepository.pageClass(orderStartTime, orderEndTime,
                pageable);

        List<BusinessStudentInfoVO> collect = page.getContent().stream().map(o -> {
            try {
                return BeanMapUtils.mapToBean(o, BusinessStudentInfoVO.class);
            } catch (Exception e) {
                e.printStackTrace();
            }
            return null;
        }).collect(Collectors.toList());
        return new PageImpl<>(collect, pageable, page.getTotalElements());
    }

    public Integer countOrder(Date startTime, Date endTime) {
        return businessStudentInfoRepository.count(startTime, endTime);
    }

    public Integer countAttach(Date startTime, Date endTime) {
        return businessStudentInfoRepository.countAttach(startTime, endTime);
    }

    public Integer countClass(Date startTime, Date endTime) {
        return businessStudentInfoRepository.countClass(startTime, endTime);
    }


    public Integer countByOrderPayTimeBetween(Date startTime, Date endTime) {
        return businessStudentInfoRepository.countByOrderPayTimeBetween(startTime, endTime);
    }

    public List<String> findOrderNo(Date startTime, Date endTime) {
        return businessStudentInfoRepository.findOrderNo(startTime, endTime).stream().map(BusinessStudentInfo::getOrderNo).collect(Collectors.toList());
    }
}

